package com.mioto.ld.module.alert.model;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @author mioto-qinxj
 * @date 2024/5/22
 * @desc
 */
@Getter
@Setter
public class LinkageTriggerDTO extends LinkageTrigger{

    @ApiModelProperty(value = "设备触发条件 触发对象为其他时,这里为空")
    private List<DeviceTriggerConditionDTO> deviceTriggerConditionDTOs;

    @ApiModelProperty(value = "定时任务触发条件 触发对象为其他时,这里为空")
    private TimedTriggerConditionDTO triggerConditionDTO;

    @ApiModelProperty(value = "编辑触发条件时传值:修改类型 1-新增 2-修改 3-删除")
    private int editType;

    public String toJsonStr(){
        if (ObjectUtil.isNotNull(deviceTriggerConditionDTOs)){
            JSONConfig jsonConfig = new JSONConfig().setOrder(true);
            JSONArray jsonArray = JSONUtil.createArray(jsonConfig);
            deviceTriggerConditionDTOs.forEach(deviceTriggerConditionDTO -> {
                jsonArray.put(deviceTriggerConditionDTO.toJsonStr());
            });
            return jsonArray.toString();
        }
        if (ObjectUtil.isNotNull(triggerConditionDTO)){
            return triggerConditionDTO.toJsonStr();
        }
        return null;
    }
}
